package flappy.VatThe;

import java.util.Random;

import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;

import flappy.Game.TaiNguyen;
import flappy.TienIch.Scrollable;

public class OngNuoc extends Scrollable {
	
	private Random r;
	
	Rectangle      dauOngNuocTren, dauOngNuocDuoi, ongTren, ongDuoi;
	
	private float  groundY;
	
	public boolean DaAnDiem = false;
	
	public OngNuoc(float x, float y, int width, int height, float scrollSpeed, float groundY) {
		super(x, y, width, height, scrollSpeed);
		r = new Random();
		dauOngNuocTren = new Rectangle();
		dauOngNuocDuoi = new Rectangle();
		ongTren = new Rectangle();
		ongDuoi = new Rectangle();
		this.groundY = groundY;
	}
	
	@Override
	public void update(float delta) {
		super.update(delta);
		
		ongTren.set(position.x, position.y, width, height);
		ongDuoi.set(position.x, position.y + height + TaiNguyen.KhoangCach2DauOng, width, groundY - (position.y + height + TaiNguyen.KhoangCach2DauOng));
		
		dauOngNuocTren.set(position.x - (TaiNguyen.DoRongDauOng - width) / 2, position.y + height - TaiNguyen.DoDaiDauOng, TaiNguyen.DoRongDauOng, TaiNguyen.DoDaiDauOng);
		dauOngNuocDuoi.set(position.x - (TaiNguyen.DoRongDauOng - width) / 2, ongDuoi.y, TaiNguyen.DoRongDauOng, TaiNguyen.DoDaiDauOng);
		
	}
	
	@Override
	// khởi tạo ống nước mới, với chiều cao ngẫu nhiên
	public void reset(float newX) {
		super.reset(newX);
		height = r.nextInt(90) + 15;
		
		DaAnDiem = false;
	}
	
	public void KhoiTaoLai(float x, float tocDoCuon) {
		velocity.x = tocDoCuon;
		reset(x);
	}
	
	public boolean KiemTraVaCham(Chim chim) {
		if (position.x < chim.x() + chim.rong) { 
			return (Intersector.overlaps(chim.bienVaCham, ongTren) 
					|| Intersector.overlaps(chim.bienVaCham, ongDuoi) 
					|| Intersector.overlaps(chim.bienVaCham, dauOngNuocTren) 
					|| Intersector.overlaps(chim.bienVaCham, dauOngNuocDuoi)
					);
			}
		return false;
	}
}
